﻿using EShopV20.Areas.Adm.ActionFilters;
using EShopV20.Areas.Adm.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EShopV20.Areas.Adm.Controllers
{
    [Security]
    public class InventoryController : Controller
    {
        EShopV20DbContext db = new EShopV20DbContext();
        //
        // GET: /Adm/Inventory/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult byCategory()
        {
            ViewBag.Title = "Inventory by categories";
            var list = db.Products.GroupBy(p => p.Category).Select(
                g => new ReportInfo
                {
                    Group = g.Key.Name,
                    Sum = g.Sum(p => p.UnitPrice * p.Quantity),
                    Count = g.Sum(p => p.Quantity),
                    Min = g.Min(p => p.UnitPrice),
                    Max = g.Max(p => p.UnitPrice),
                    Avg = g.Average(p => p.UnitPrice)
                });
            return View("Inventory", list);
        }

        public ActionResult bySupplier()
        {
            ViewBag.Title = "Inventory by suppliers";
            var list = db.Products.GroupBy(p => p.Supplier).Select(
                g => new ReportInfo
                {
                    Group = g.Key.Name,
                    Sum = g.Sum(p => p.UnitPrice * p.Quantity),
                    Count = g.Sum(p => p.Quantity),
                    Min = g.Min(p => p.UnitPrice),
                    Max = g.Max(p => p.UnitPrice),
                    Avg = g.Average(p => p.UnitPrice)
                });
            return View("Inventory", list);
        }
	}
}